package com.kamistoat.meimeistore.product.aliyunVideo.controller;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.kamistoat.common.To.AliyunVideoTo.SaveAliVideoInfoTo;
import com.kamistoat.common.To.AliyunVideoTo.VideoUploadAuthRequestTo;
import com.kamistoat.common.To.AliyunVideoTo.VideoUploadAuthResponseTo;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;
import com.kamistoat.meimeistore.product.aliyunVideo.service.GetVideoInfoAuthService;
import com.kamistoat.meimeistore.product.aliyunVideo.service.GetVideoUploadService;
import com.kamistoat.meimeistore.product.entity.VideoEntity;
import com.kamistoat.meimeistore.product.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("meimeistoreproduct/aliyunVideo")
public class AliyunVideoController {

    @Autowired
    GetVideoInfoAuthService getVideoInfoAuthService;
    @Autowired
    GetVideoUploadService getVideoUploadService;
    @Autowired
    VideoService videoService;

    /**
     * controller方法，获取视频播放地址。因为实际项目中都是使用播放凭证，所以这里就不做返回值了
     *
     * @param videoId videoId
     * @throws ClientException
     */
    @GetMapping("/getvideoinfo")
    public void getVideoInfo(@RequestParam("videoId") String videoId) throws ClientException {
        GetPlayInfoResponse videoInfoRes = getVideoInfoAuthService.getVideoInfo(videoId);
        // 从GetPlayInfoResponse中获取信息
        List<GetPlayInfoResponse.PlayInfo> playInfoList = videoInfoRes.getPlayInfoList();
        //播放地址。因为同一个视频可以有多个播放地址，比如不同清晰度/码率/加密
        List<String> playURLList = new ArrayList<>();
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            playURLList.add(playInfo.getPlayURL());
        }
        //Base信息
        System.out.print("VideoBase.Title = " + videoInfoRes.getVideoBase().getTitle() + "\n");
    }

    /**
     * 获取视频播放凭证
     *
     * @param videoId videoId
     * @return 视频播放凭证。该凭证必须使用阿里云提供的播放器API才能播放
     * @throws ClientException
     */
    @GetMapping("/getvideoauth")
    public String getVideoAuth(@RequestParam("videoId") String videoId) throws ClientException {
        GetVideoPlayAuthResponse videoAuthRes = getVideoInfoAuthService.getVideoAuth(videoId);
        // 从GetVideoPlayAuthResponse中获取信息
        // 一个视频只会有一个视频凭证，所以这里不是用list
        System.out.print("PlayAuth = " + videoAuthRes.getPlayAuth() + "\n");
        //VideoMeta信息
        System.out.print("VideoMeta.Title = " + videoAuthRes.getVideoMeta().getTitle() + "\n");
        return videoAuthRes.getPlayAuth();
    }

    /**
     * 获取视频上传地址和凭证
     *
     * @param videoUploadAuthRequestTo 前端传递的json数据，使用这个To类进行映射
     * @return 返回R对象，内部包装To类
     */
    @RequestMapping("/getvideoupload")
    public R getVideoUpload(@RequestBody VideoUploadAuthRequestTo videoUploadAuthRequestTo) throws ClientException {
        VideoUploadAuthResponseTo videoUploadTo = getVideoUploadService.getVideoUpload(videoUploadAuthRequestTo);
        return R.ok().put("responseTo", videoUploadTo);
    }

    /**
     * 当上传暂停时，重新恢复上传时，需要根据前端已经得到的videoId来重新获取上传地址和凭证
     *
     * @param videoId 暂停上传的videoId
     * @return
     */
    @GetMapping("/refreshvideoupload")
    public R refreshVideoUpload(@RequestParam("videoId") String videoId) throws ClientException {
        VideoUploadAuthResponseTo videoUploadTo = getVideoUploadService.refreshVideoUpload(videoId);
        return R.ok().put("responseTo", videoUploadTo);
    }


    /**
     * 前端直连阿里上传视频后，将视频信息发送给后端保存到数据库
     *
     * @param infoTo
     * @return
     */
    @RequestMapping("/savealivideoinfo")
    public R saveAliVideoInfo(@RequestBody SaveAliVideoInfoTo infoTo) {
        R res = getVideoUploadService.saveAliVideoInfo(infoTo);
        return res == null ? R.error("保存异常") : res;
    }

    /**
     * 获取数据库中的所有视频信息
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = videoService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 仅仅修改数据库中视频的状态
     */
    @RequestMapping("/update/status")
    public R updateStatus(@RequestBody VideoEntity videoEntity) {
        videoService.updateById(videoEntity);
        return R.ok();
    }

    /**
     * 删除数据库和视频源文件
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] vids) throws ClientException {
        videoService.removeVideoByIds(Arrays.asList(vids));
        return R.ok();
    }


    /**
     * 从数据库中获取视频信息
     */
    @RequestMapping("/info/{vid}")
    public R info(@PathVariable("vid") Long vid) {
        VideoEntity video = videoService.getById(vid);
        return R.ok().put("video", video);
    }

    /**
     * 如果修改了视频标题/描述等基础信息，则需要同时更改数据库和源文件
     *
     * @param videoEntity
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody VideoEntity videoEntity) throws ClientException {
        R res = videoService.updateVideoInfo(videoEntity);
        return res == null ? R.error("发生错误") : res;
    }


}
